﻿namespace EasyWeb.Web
{
    using System;
    using System.ComponentModel;
    using System.ComponentModel.Composition;
    using System.Web;
    using System.Web.Mvc;
    using EasyWeb.Extensions;
    using EasyWeb.Services;

    public class EWHttpApplication : HttpApplication
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public IEWCultureService cultureService;

        public EWHttpApplication()
        {
            EWExtensionManager.Initialize(null);
            EWExtensionManager.CompositionContainer.SatisfyImportsOnce(this);

            this.BeginRequest += async delegate
            {
                await this.cultureService.InitializePerRequestAsync();
            };
        }

        protected virtual void OnApplicationStart()
        {
            InitializeAreas();
            InitializeWebApi();
            InitializeRoutes();
        }

        protected virtual void OnApplicationEnd()
        {
        }

        protected virtual void InitializeAreas()
        {
            AreaRegistration.RegisterAllAreas();
        }

        protected virtual void InitializeWebApi()
        {
        }

        protected virtual void InitializeRoutes()
        {
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        protected void Application_Start(object sender, EventArgs e)
        {
            OnApplicationStart();
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        protected void Application_End(object sender, EventArgs e)
        {
            OnApplicationEnd();
        }
    }
}
